<?php
/*
  Template Name: Profile
 */
get_template_part('secondary', 'header');
get_template_part('secondary', 'sidebar');

$user = wp_get_current_user();
$user_id = $user->ID;

$current_workers = $wpdb->get_var($wpdb->prepare("select number from bpa_registry_workers where user_id = %d order by year desc, month desc limit 1", $user_id));

$current_product = $wpdb->get_var($wpdb->prepare("select product from bpa_registry_product where user_id = %d order by year desc, month desc limit 1", $user_id));

$workers_year_list = $wpdb->get_col($wpdb->prepare("select distinct year from bpa_registry_workers where user_id = %d order by year asc", $user_id));

$current_sector_data = $wpdb->get_row($wpdb->prepare("
select r.user_id, s.id as sector_id, s.name as sector_name, sb.id as subsector_id, sb.name as subsector_name, r.year, r.month
from bpa_registry_sector r
inner join bpa_company_sector s
on r.sector_id = s.id
inner join bpa_company_subsector sb
on r.subsector_id = sb.id
where user_id = %d order by year desc, month desc limit 1",
$user_id));
$sector_year_list = $wpdb->get_col($wpdb->prepare("select distinct year from bpa_registry_sector where user_id = %d order by year asc", $user_id));

$product_year_list = $wpdb->get_col($wpdb->prepare("select distinct year from bpa_registry_product where user_id = %d order by year asc", $user_id));

$inventory_user = $wpdb->get_results($wpdb->prepare(
        "select it.id, it.name, u.quantity from bpa_inventory_user u
        left join bpa_inventory_item it
        on u.item_id = it.id
        where u.user_id = %d
        order by it.name asc",
        $user_id));

?>

<div id="dash-content">
    
    <header id="profile-header">
        <h1>PERFIL</h1>
    </header>	
    
    <div id="profile-data">
        <div class="profile-item">
            <div class="profile-item-title">
                <h2>RUC</h2>
            </div>
            <div class="profile-item-content">
                <div id="ruc_static" class="profile-item-static">
                    <?php echo get_user_meta($user_id, 'ruc_profile', true); ?>
                </div>
            </div>
        </div>
        
        <div class="profile-item workers-item">
            <div class="profile-item-title">
                <h2>Colaboradores</h2>
                <h4 style="margin-bottom: 10px;margin-top: -5px;">(Obreros, empleados, etc)</h4>
            </div>
            <div class="profile-item-content">
                <div id="colaborator_static" class="profile-item-static">
                    <div class="profile-span">
                        <span id='colaborator_value_static'><?php echo $current_workers; ?></span>
                    </div>
                    <a class="mini-buton" id="edit_number_colaborator">editar</a>
                </div>
                <div id="colaborator_dynamic" class="profile-item-dynamic hidden">
                    <div class="select-input year-input">
                        <select id="colaborator-year-selector">
                            <?php foreach($workers_year_list as $year) { ?>
                                <option value="<?php echo $year; ?>" ><?php echo $year; ?></option>
                            <?php } ?>
                        </select>
                    </div>
                    <div class="select-input month-input">
                        <select id="colaborator-month-selector">
                        </select>
                    </div>
                    <div class="box-input workers-input">
                        <input type="text" class="mask_colaboradores" id="colaborator_value" value="<?php echo $current_workers; ?>">
                    </div>
                    <div class="clear">
                        <a class="mini-buton save"  id="colaborator_save" >guardar</a>
                        <a class="mini-buton cancel"  id="colaborator_cancel" >cancelar</a>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="profile-item sector-item">
            <div class="profile-item-title">
                <h2>RUBRO Y SUB RUBRO</h2>
            </div>
            <div class="profile-item-content">
                <div id="company_sector_static" class="profile-item-static">
                    <div class="company_sector_value">
                        <span id='company_sector_value_static'>
                            <?php echo $current_sector_data->sector_name; ?>
                        </span>
                        <br/><br/>
                        <span id='company_subsector_value_static'>
                            <?php echo $current_sector_data->subsector_name; ?>
                        </span>
                    </div>
                    <a class="mini-buton" id="edit_number_company_sector">editar</a>
                </div>
                <div id="company_sector_dynamic" class="profile-item-dynamic hidden">
                    <div class="select-input year-input">
                        <select id="company-sector-year-selector">
                            <?php foreach($sector_year_list as $year) { ?>
                                <option value="<?php echo $year; ?>"><?php echo $year; ?></option>
                            <?php } ?>
                        </select>
                    </div>
                    <div class="select-input month-input">
                        <select id="company-sector-month-selector">
                        </select>
                    </div>
                    <div class="select-input company-sector-input">
                        <select id="company-sector-selector">
                        </select>
                    </div>
                    <div class="select-input company-subsector-input">
                        <select id="company-subsector-selector">
                        </select>
                    </div>
                    
                    <div class="clear">
                        <a class="mini-buton save"  id='company_sector_save' >guardar</a>
                        <a class="mini-buton cancel"  id='company_sector_cancel' >cancelar</a>
                    </div>                    
                </div>
            </div>
        </div>
        
        <div class="profile-item product-item">
            <div class="profile-item-title">
                <h2>UNIDAD PRODUCIDA</h2>
                <h4 style="margin-bottom: 10px;margin-top: -5px; width:400px">Unidad estándar para una PYME a partir de la cual se puede representar las ventas de la misma. Detalle cuál es la Unidad Producida que mejor se ajusta a las actividades / rubro de su PYME en un mes convencional.
Por ejemplo: metros de tela, litros de leche, kilogramos de café, horas de consultoría facturadas, etc.</h4>
            </div>
            <div class="profile-item-content">
                <div id="product_static" class="profile-item-static">
                    <div class="profile-span">
                        <span id='product_value_static'><?php echo $current_product; ?></span>
                    </div>
                    <a class="mini-buton" id="edit_product">editar</a>
                </div>
                <div id="product_dynamic" class="profile-item-dynamic hidden">
                    <div class="select-input year-input">
                        <select id="product-year-selector">
                            <?php foreach($product_year_list as $year) { ?>
                                <option value="<?php echo $year; ?>"><?php echo $year; ?></option>
                            <?php } ?>
                        </select>
                    </div>
                    <div class="select-input month-input">
                        <select id="product-month-selector">
                        </select>
                    </div>
                    <div class="box-input product-input">
                        <input type="text" id="product_value" maxlength="100" value="<?php echo $current_product; ?>">
                    </div>                    
                    <div class="clear">
                        <a class="mini-buton save"  id='product_save' >guardar</a>
                        <a class="mini-buton cancel"  id='product_cancel' >cancelar</a>
                    </div>                    
                </div>
            </div>
        </div>
        
        <div class="profile-item password-item">
            <div class="profile-item-title">
                <h2>Contraseña</h2>
            </div>
            <div class="profile-item-content">
                <div id="password_static" class="profile-item-static">
                    <div class="p-right">
                        <div class="profile-span">
                            <span>● ● ● ● ● ● ● ●</span>
                        </div>
                        <a class="mini-buton" id="edit_password">editar</a>
                    </div>
                </div>
                <div id="password_dynamic" class="profile-item-dynamic hidden">
                    <div class="pass-active">
                        <div class="correo-input">
                            <h5>Antigua contraseña</h5>
                            <div class="box-input">
                                <input type="password" id="edit_password_old">
                            </div>
                        </div>
                        <div class="correo-input">
                            <h5>Nueva contraseña</h5>
                            <div class="box-input">
                                <input type="password" id="edit_password_new">
                            </div>
                        </div>
                        <div class="correo-input">
                            <h5>Repita nueva contraseña</h5>
                            <div class="box-input">
                                <input type="password" id="edit_password_confirm">
                            </div>
                        </div>
                    </div>
                    <div class="clear">
                        <a class="mini-buton save" id='password_save'>guardar</a>
                        <a class="mini-buton cancel" id='password_cancel'>cancelar</a>
                    </div>
                </div>
                <div class="clear pass-active">
                    <span id="password_error_message" class="smallfont"></span>
                    <span id="password_success_message" class="smallfont"></span>
                </div>
            </div>
        </div> 
        <div class="profile-item email-item">
            <div class="profile-item-title">
                <h2>Correo electrónico</h2>
            </div>
            <div class="profile-item-content">
                <div id="email_static" class="profile-item-static">
                    <div class="p-right">
                        <div class="profile-span">
                            <span id='email_value_static'><?php echo $user->user_email; ?></span>
                        </div>
                        <a class="mini-buton" id="edit_email">editar</a>
                    </div>
                </div>
                <div id="email_dynamic" class="profile-item-dynamic hidden">
                    <div class="clear"></div>
                    <div class="pass-active">
                        <div class="correo-input">
                            <h5>Antiguo correo electrónico</h5>
                            <div class="box-input">
                                <input type="text" id="edit_email_old">
                            </div>
                        </div>
                        <div class="correo-input">
                            <h5>Nuevo correo electrónico</h5>
                            <div class="box-input">
                                <input type="text" id="edit_email_new">
                            </div>
                        </div>
                        <div class="correo-input">
                            <h5>Repita correo electrónico</h5>
                            <div class="box-input">
                                <input type="text" id="edit_email_confirm">
                            </div>
                        </div>
                    </div>
                    <div class="clear">
                        <a class="mini-buton save" id='email_save'>guardar</a>
                        <a class="mini-buton cancel" id='email_cancel'>cancelar</a>
                    </div>
                </div>
                <div class="clear pass-active">
                    <span class="hidden smallfont" id="email_error_message" ></span>
                    <span class="hidden smallfont" id="email_success_message"></span>
                </div>
            </div>
        </div> 
        <div class="profile-item inventory-item">
            <div class="profile-item-title">
                <h2>Inventario</h2>
                 <h4 style="margin-bottom: 10px;margin-top: -5px; width:400px">Por favor ingrese los equipos con los que cuenta su empresa, para poder mostrar los montos referenciales de ahorro que podrían generarse.
</h4>
            </div>
            <div class="profile-item-content">
                <div id="inventory_static" class="profile-item-static">
                    <div class="p-right">		
                        <a class="mini-buton" id="edit_inventory">editar</a>
                    </div>
                </div>
                <div id="inventory_list">
                    <?php 
                        $inventory_length = count($inventory_user);
                        $first_column_length = $inventory_length - ((int) ($inventory_length / 2)); ?>
                        <div class="left-column">
                            <ul>
                                <?php
                                $i = 0;
                                foreach ($inventory_user as $item) {
                                    $i++;
                                    $id = $item->id;
                                    $name = $item->name;
                                    $value = isset($item->quantity) ? $item->quantity : 0; 
                                    ?>
                                    <li>
                                        <span class="number">
                                            <div>
                                                <input id="inventory_<?php echo $id; ?>" readonly="readonly" class="inventory_count mask_inventario" type="text" name="inventory[<?php echo $id; ?>]" value="<?php echo $value ?>"/>
                                            </div>
                                        </span>
                                        <?php echo $name; ?>
                                    </li>
                                    <?php
                                    if ($i == $first_column_length) {
                                        ?>
                                    </ul>
                        </div>
                        <div class="right-column">
                                    <ul>
                                        <?php
                                    }
                                }
                                ?>
                            </ul>
                        </div>
                </div>
                

                <div id="inventory_dynamic" class="hidden">
                    <div class="clear"></div>
                    <a class="add-item-button" id='inventory_add'>+ Agregar item</a>
                    <div id="customized-items-template" style="display:none;">
                        <div class="customized-item">
                            <div class="qty"><span class="label">Cantidad</span><div class="box-input"><input type="text" value="" min="0" step="1" onkeypress="return justNumbers(event);" /></div></div>
                            <div class="name"><span class="label">Nombre</span><div class="box-input"><input type="text" value="" /></div></div>
                            <div class="type"><span class="label">Tipo</span>
                                <select class="select-input">
                                    <option value="1">Agua</option>
                                    <option value="2">Electricidad</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div id="customized-items"></div>
                    <div class="clear">
                        <a class="mini-buton save" id='inventory_save'>guardar</a>
                        <a class="mini-buton cancel" id='inventory_cancel'>cancelar</a>
                    </div>
                </div>
                <div class="clear pass-active">
                    <span class="hidden smallfont" id="inventory_error_message" ></span>
                    <span class="hidden smallfont" id="inventory_success_message"></span>
                </div>
            </div>
        </div> 
        <!--<div class="profile-item">
            <div class="profile-item-title">
                <h2>Editar Información</h2>
            </div>
            <div class="clear"></div>
            <p>Si deseas editar otra información de tu empresa, comunícate con nosotros.</p>
            <a href="<?php echo Helpers::get_user_contact_url(); ?>" class="buton">Contacto</a>
        </div>-->
    </div>
</div>
<?php get_template_part('secondary', 'footer'); ?>